#import "SadFaceGame.h"

@interface SadFaceGame ()

@property NSArray* colors;
@property BOOL isGameOver;
@property NSArray* faceColours;
@property NSInteger numberOfSadFace;

@end

@implementation SadFaceGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"SadFaceGame"];
        [self setType:reaction];
        self.ruleOfTheGame=@"Is there a sad face?";
        self.faceColours=[[NSArray alloc]initWithObjects:@"smileyYellow",@"smileyGreen.png" ,@"smileyRed.png",@"smileyBlue", nil];
        
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
    }
    return self;
}


-(void)draw{
    [self removeAllComponents];
    
    
    self.numberOfSadFace=0;

    
    for (int coloum=0; coloum<6; coloum++) {
        for (int row=0; row<6; row++) {
            
            int faceColour=arc4random_uniform(5);
            
            NSInteger imageSize=(kScreenWidth-kHeighMargin)/7;
            
            if (faceColour == 4 && self.numberOfSadFace==8) {
                int transform=arc4random_uniform(4);
                CGFloat trans=kTransKoef * transform;
                ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:@"sad.png" andFrame:CGRectMake(kHeighMargin+(coloum*imageSize), 10+(row*imageSize), imageSize, imageSize)andTrans:trans];
                [self addComponents:image];
                
            }
            else{
                if (faceColour==4) {
                    self.numberOfSadFace++;
                    faceColour=0;
                }
                NSString* face=[self.faceColours objectAtIndex:faceColour];
                int transform=arc4random_uniform(4);
                CGFloat trans=kTransKoef * transform;
                ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:face andFrame:CGRectMake(kHeighMargin +(coloum*imageSize), 10+(row*imageSize), imageSize, imageSize)andTrans:trans];
                [self addComponents:image];
            }
            
        }
    }
    
    
    
}

-(BOOL)isRight{
    if (self.numberOfSadFace>=8) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
     [NSTimer scheduledTimerWithTimeInterval: 2                                                 target: self selector:@selector(newFace:)userInfo: nil repeats:YES];
    
}

-(void)newFace:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}

@end
